package com.zhiwei.common.constant;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.Data;

/**
 * @author ZHIWEI.YANG
 * @time 2019/11/10 - 19:39
 * @decription controller响应体结构
 */
@Data
public final class ResultResponseMsg {

    /**
     * 响应状态码
     */
    private String code;

    /**
     * 提示信息
     */
    private String msg;

    /**
     * 响应数据
     */
    private Object data;

    /**
     * 构建失败响应体结构
     *
     * @return
     */
    public static ResultResponseMsg buildFailureResultMsg() {
        return buildResultMsg(PublishConstant.FAILURE_CODE, PublishConstant.FAILURE_MSG, null);
    }

    /**
     * 构建失败响应体结构
     *
     * @param data 响应数据
     * @return
     */
    public static ResultResponseMsg buildFailureResultMsg(Object data) {
        return buildFailureResultMsg(PublishConstant.FAILURE_MSG, data);
    }

    /**
     * 构建失败响应体结构
     *
     * @param data 响应数据
     * @return
     */
    public static ResultResponseMsg buildFailureResultMsg(String msg, Object data) {
        return buildResultMsg(PublishConstant.FAILURE_CODE, msg, data);
    }

    /**
     * 构建成功响应体结构
     *
     * @return
     */
    public static ResultResponseMsg buildSuccessResultMsg() {
        return buildSuccessResultMsg(null);
    }

    /**
     * 构建成功响应体结构
     *
     * @param data 响应数据
     * @return
     */
    public static ResultResponseMsg buildSuccessResultMsg(Object data) {
        return buildResultMsg(PublishConstant.SUCCESS_CODE, PublishConstant.SUCCESS_MSG, data);
    }

    /**
     * 构建通用响应体结构
     *
     * @param errorEnum 错误码枚举
     * @param data      响应数据
     * @return
     */
    public static ResultResponseMsg buildResultMsg(ErrorEnum errorEnum, Object data) {
        return buildResultMsg(errorEnum.code, errorEnum.msg, data);
    }

    /**
     * 构建通用响应体结构
     *
     * @param code 状态码
     * @param msg  状态说明
     * @param data 响应数据
     * @return
     */
    public static ResultResponseMsg buildResultMsg(String code, String msg, Object data) {
        ResultResponseMsg resultResponseMsg = new ResultResponseMsg();
        resultResponseMsg.setCode(code);
        resultResponseMsg.setMsg(msg);
        resultResponseMsg.setData(data);
        return resultResponseMsg;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this, SerializerFeature.WriteMapNullValue);
    }
}
